package io.renren.modules.store.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.store.entity.TStoreBrandEntity;
import io.renren.modules.store.service.TStoreBrandService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 02:05:21
 */
@RestController
@RequestMapping("store/tstorebrand")
public class TStoreBrandController extends AbstractController {
    @Autowired
    private TStoreBrandService tStoreBrandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorebrand:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("key",params.get("key"));
        PageUtils page = tStoreBrandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    @RequiresPermissions("store:tstorebrand:info")
    public R info(@PathVariable("brandId") Long brandId){
		TStoreBrandEntity tStoreBrand = tStoreBrandService.getById(brandId);
        return R.ok().put("tStoreBrand", tStoreBrand);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorebrand:save")
    public R save(@RequestBody TStoreBrandEntity tStoreBrand){
        TStoreBrandEntity one = tStoreBrandService.getOne(new QueryWrapper<TStoreBrandEntity>()
                .eq("seller_id", getSeller())
                .eq("brand_no", tStoreBrand.getBrandNo())
                .or()
                .eq("brand_name", tStoreBrand.getBrandName())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("餐牌名称或编号存在重复项，请重新输入");
        }
        tStoreBrand.setSellerId(getSeller());
		tStoreBrandService.save(tStoreBrand);

        return R.ok();
    }

    @NoRepeatSubmit
    @PostMapping("saveBatch")
    public R saveBatch(@RequestBody Map<String,Object> map){
        Long max = tStoreBrandService.getMaxBrand(getSeller());
        List<TStoreBrandEntity> list = new ArrayList<>();
        if (max != null){
            for (int i = 0 ; i < Integer.parseInt(map.get("brandNum").toString()) ; i++){
                TStoreBrandEntity storeBrandEntity = new TStoreBrandEntity();
                storeBrandEntity.setSellerId(getSeller());
                storeBrandEntity.setBrandNo(max+i+1);
                if (map.get("prefix") != null){
                    storeBrandEntity.setBrandName(map.get("prefix").toString()+(max+i+1)+map.get("company").toString());
                }
                else {
                    storeBrandEntity.setBrandName((max+i+1)+map.get("company").toString());
                }
                storeBrandEntity.setStatus(1);
                list.add(storeBrandEntity);
            }
        }else {
            for (int i = 0 ; i < Integer.parseInt(map.get("brandNum").toString()) ; i++){
                TStoreBrandEntity storeBrandEntity = new TStoreBrandEntity();
                storeBrandEntity.setSellerId(getSeller());
                storeBrandEntity.setBrandNo((long) i+1);
                if (map.get("prefix") != null){
                    storeBrandEntity.setBrandName(map.get("prefix").toString()+(i+1)+map.get("company").toString());
                }
                else {
                    storeBrandEntity.setBrandName((i+1)+map.get("company").toString());
                }
                storeBrandEntity.setStatus(1);
                list.add(storeBrandEntity);
            }
        }
        tStoreBrandService.saveBatch(list);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorebrand:update")
    public R update(@RequestBody TStoreBrandEntity tStoreBrand){
		tStoreBrandService.updateById(tStoreBrand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorebrand:delete")
    public R delete(@RequestBody Long[] brandIds){
		tStoreBrandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
